#! /usr/bin/env python
# -*- coding: utf-8

##	  Copyright 2012, Geoffrey GROFF and Dimitri SEGARD
##	  This file is part of otaku-loader.
##
##    otaku-loader is free software: you can redistribute it and/or modify
##    it under the terms of the GNU General Public License as published by
##    the Free Software Foundation, either version 3 of the License, or
##    (at your option) any later version.
##
##    otaku-loader is distributed in the hope that it will be useful,
##    but WITHOUT ANY WARRANTY; without even the implied warranty of
##    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
##    GNU General Public License for more details.
##
##    You should have received a copy of the GNU General Public License
##    along with otaku-loader.  If not, see <http://www.gnu.org/licenses/>.

from PyQt4 import QtCore
import threading
import time

import parse
import globales

def thread_func():
	thread_fiche=threading.currentThread()
	fiches = []
	
	while not fiches:
		fiches = parse.getFiches()
		time.sleep(1)

	for fiche in fiches:
		if not thread_fiche.stop:
			globales.dl.getFicheInfos(fiche)
			time.sleep(0.01)
		else:
			break


thread_fiche=threading.Thread(target=thread_func)
			
def run():
	global thread_fiche
	if thread_fiche.is_alive():
		thread_fiche.stop=True
		thread_fiche.join()
	
	thread_fiche = None
	thread_fiche=threading.Thread(target=thread_func)
	
	thread_fiche.stop=False
	thread_fiche.start()

def stop():
	global thread_fiche
	if thread_fiche.is_alive():
		thread_fiche.stop=True
		thread_fiche.join()
